﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Brc_PaaS.Common.Helper
{
    public static class DateTimeHelper
    {
        /// <summary>
        /// 将时间转换成long
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static long ConvertDatetTimeToLong(this DateTime dateTime)
        {
            DateTime dtStart = new DateTime(1970, 1, 1, 0, 0, 0);
            TimeSpan toNow = dateTime.Subtract(dtStart);
            long timeStamp = toNow.Ticks;
            timeStamp = long.Parse(timeStamp.ToString().Substring(0, timeStamp.ToString().Length - 4));
            return timeStamp;
        }
        /// <summary>
        /// 将long转换成DateTime
        /// </summary>
        /// <param name="timeSpan"></param>
        /// <returns></returns>
        public static DateTime ConvertLongToDateTime(this long timeSpan)
        {
            DateTime dtStart = new DateTime(1970, 1, 1, 0, 0, 0);
            long lTime = long.Parse(timeSpan + "0000");
            TimeSpan toNow = new TimeSpan(lTime);
            DateTime dtResult = dtStart.Add(toNow);
            return dtResult;
        }
        
        /// <summary>
        /// 返回给定时间起始的毫秒数
        /// </summary>
        /// <param name="now">要计算的时间（UTC时间）</param>
        /// <returns></returns>
        public static long GetCurrnetTimeMillis(this DateTime now)
        {
            DateTime starttime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);            
            return Convert.ToInt64((now - starttime).TotalMilliseconds);
        }
    }
}
